package com.test.controller;

import com.sun.xml.internal.rngom.parse.Context;
import com.test.po.User;
import com.test.po.UserExample;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@SessionAttributes({"username","uid"})
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login.do")
    public String login(String username, String password, ModelMap map){
        User user = userService.findByUsername(username);
        if(user != null){
            if (user.getPassword().equals(password)){
                System.out.println("login success");
                map.addAttribute("username",user.getUsername());
                map.addAttribute("uid",user.getUid());
                return "forward:toIndex.do";
            }else{
                System.out.println("wrong password");
                return "forward:error1.jsp";
            }
        }else{
            System.out.println("user not exist");
            return "forward:error1.jsp";
        }
    }

    @RequestMapping("/register.do")
    public String register(User user, Model model, HttpServletResponse response) throws IOException {

        User user1 = userService.findByUsername(user.getUsername());
        if (user1 != null) {
            System.out.println("true");
            //输出true
            return "forward:error.jsp";
        }else{
            userService.insert(user);
            return "registersuccess";
        }
    }
    @RequestMapping("/adregister.do")
    public String adregister(User user, Model model){
        userService.insert(user);
        return "uadd";
    }
    @RequestMapping("/udelete.do")
    public String udelete(Integer uid){
        userService.deleteByPrimaryKey(uid);
        return "forward:forulist.do";
    }





}
